<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<head>
<c:if test="${fn:contains(sessionInfo.resourceList, '/chargeXRay/edit')}">
	<script type="text/javascript">
		$.canEdit = true;
	</script>
</c:if>

<script>
	var registerCharges = [];
	var registerId = "${registerId}";
	
	$('#xRay').combobox({    
	    url:'${ctx}/xRay/list',    
	    valueField:'id',    
	    textField:'partOfBody'   
	}); 
	
</script>

<c:forEach items="${registerCharges}" var="registerCharge">
	<script type="text/javascript">
		var registerCharge = {
			'id':"${registerCharge.id}",
			'partOfBody': "${registerCharge.partOfBody}",
			'addTime': "${registerCharge.addTime}",
			'chargeTime':"${registerCharge.chargeTime}",
			'xRayAddTime':"${registerCharge.xRayAddTime}",
			'imageSrc':"${registerCharge.imageSrc}"
		};

		registerCharges.push(registerCharge);
	</script>
</c:forEach>

<script type="text/javascript">
	
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			striped : true,
			rownumbers : true,
			singleSelect : true,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'asc',
			columns :[[{
				width : '100',
				title : '身体部位',
				field : 'partOfBody'
			},{
				width : '100',
				title : 'id',
				field : 'id',
				hidden: true
			}, {
				width : '150',
				title : '开单时间',
				field : 'addTime',
			},{
				width : '150',
				title : '缴费时间',
				field : 'chargeTime'
			}, {
				width : '150',
				title : '拍片时间',
				field : 'xRayAddTime'
			},{
				field : 'viewImageSrc',
				title : '拍片图像',
				width : 80,
				formatter : function(value, row, index) {
					var str = '&nbsp;';
					str += $.formatString('<a href="${ctx}/{0}" target="_blank"><img src="${ctx}/{1}" width="45"/></a>', row.imageSrc,row.imageSrc);
					return str;
				}
			}, {
				field : 'action',
				title : '操作',
				width : 200,
				formatter : function(value, row, index) {
					var str = '<br/>';
					if(row.chargeTime == '' && $.canEdit) {
						str += '<a href="javascript:void(0)" onclick="charge(\'{0}\');" >缴费</a><br/><br/>';
					}
					if(row.chargeTime != '' && row.xRayAddTime == '') {
						str  += "<form id=\"xRayForm" + row.id + "\"method=\"post\" enctype=\"multipart/form-data\">"+
						"<a href=\"javascript:void(0)\" onclick=\"addXRay(\'{1}\');\" >拍片</a>" +
						"&nbsp;&nbsp;&nbsp;&nbsp;" + 
						"<input name=\"xRay" + row.id + "\" type=\"file\">" + 
					"</form><br/>";
					}
					str = $.formatString(str, row.id,row.id);
					return str;
				}
			} ] ],
			data: registerCharges,
			toolbar : '#toolbar'
		});
	});
	
	function addCharge() {
		var xRayId = $('#xRay').combobox('getValue');
		if(xRayId == '') {
			$.messager.alert('错误', '未选择放射的身体部位', 'error');
		} else {
			var registerId = "${registerId}";
			
			$.ajax({
				type: "POST",
				async: false,
				data:  {
					'xRayId':xRayId,
					'registerId':registerId
				},
		        url: '${ctx}/chargeXRay/addCharge',
		        dataType: "json",
		        success:function(result) {
		        	if(result.status.errorCode == 0) {
		        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
		        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/chargeXRay/detailPage?registerId=' + registerId);
		        		});
		        	} else {
		        		$.messager.alert('错误', result.status.errorMsg, 'error');
		        	}
		        },
		        error:function(result) {
		        }
	   	 	});
		}
	}
	
	function charge(registerChargeId) {
		$.ajax({
			type: "POST",
			async: false,
			data:  {
				'registerChargeId':registerChargeId
			},
	        url: '${ctx}/chargeXRay/charge',
	        dataType: "json",
	        success:function(result) {
	        	if(result.status.errorCode == 0) {
	        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
	        			debugger;
	        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/chargeXRay/detailPage?registerId=' + registerId);
	        		});
	        	} else {
	        		$.messager.alert('错误', result.status.errorMsg, 'error');
	        	}
	        },
	        error:function(result) {
	        }
   	 	});
	}
	
	function addXRay(registerChargeId) {
		debugger;
		if( $('input[name=\'xRay' + registerChargeId + '\']').val() == '') {
			$.messager.alert('错误', '未选择文件', 'error');
		} else {
			$('#xRayForm' + registerChargeId).form({
				url : '${ctx}/chargeXRay/xRay?registerChargeId=' + registerChargeId,
				onSubmit : function() {
					progressLoad();
					var isValid = $(this).form('validate');
					if (!isValid) {
						progressClose();
					}
					return isValid;
				},
				success : function(result) {
					progressClose();
					result = $.parseJSON(result);
					if (result.status.errorCode == 0) {
						$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
		        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/chargeXRay/detailPage?registerId=' + registerId);
		        		});
					} else {
						parent.$.messager.alert('错误', result.result.errorMsg, 'error');
					}
				}
			});
			
			$('#xRayForm' + registerChargeId).submit();
		}
		
	}
</script>
</head>

<div class="easyui-layout" data-options="fit:true,border:false">
	
	<div data-options="region:'center',fit:true,border:false">
		<table id="dataGrid" data-options="fit:true,border:false"></table>
	</div>
	
	<div id="toolbar" style="display: none;">
			<a onclick="addCharge();" href="javascript:void(0);"
				class="easyui-linkbutton"
				data-options="plain:true,iconCls:'icon_add'">开单新拍片</a>
			
			<select id="xRay" style="width: 140px; height: 29px;"></select>
			
	</div>
</div>
